<template>
  <div class="class-select">
    <el-drawer
      v-model="visible"
      :title="`我的${authStore.nowIsSmartRoomCode ? '爱学班' : route.name === 'teaching' ? '教学班' : '行政班'}`"
    >
      <ul class="class-list">
        <li
          class="item"
          :class="{ active: selectedClass.id === item.id }"
          v-for="item in classList"
          :key="item"
          @click="selectedClass = item"
        >
          <p class="subject-box">
            <label
              class="subject"
              v-for="item1 in (item.subject_list || []).slice(0, 3)"
              :key="item1.code"
              >{{ item1.name }}</label
            >
          </p>

          <span class="people"
            ><el-icon>
              <User />
            </el-icon>
            {{ item.members }}</span
          >
          <p class="info">
            <img src="@/assets/images/class-manage/class-icon.svg?url" />
            {{ item.grade_name }}{{ item.class_name }}
          </p>
        </li>
      </ul>
      <div class="btn-box">
        <button class="btn cancel" @click="cancelSelectClass">取消</button>
        <button class="btn confirm" @click="handleSelectClass">确认</button>
      </div>
    </el-drawer>
  </div>
</template>
<script setup>
import { User } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useAuthStore } from '@/stores'

const props = defineProps({
  classList: {
    default: () => [],
    type: Array
  },
  currentClass: {
    default: () => {},
    type: Object
  }
})

const authStore = useAuthStore()
const route = useRoute()
const emits = defineEmits(['select-class'])
const visible = defineModel()
const selectedClass = ref(0)

const handleSelectClass = () => {
  emits('select-class', selectedClass.value)
  visible.value = false
}

const cancelSelectClass = () => {
  visible.value = false
}

watch(
  () => props.currentClass,
  () => {
    selectedClass.value = props.currentClass
  },
  {
    deep: true,
    immediate: true
  }
)
</script>
<style lang="less" scoped>
.class-select {
  .class-list {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    flex: 1;
    overflow-y: auto;
    align-content: flex-start;

    .item {
      display: flex;
      align-items: center;
      box-sizing: border-box;
      width: 236px;
      height: 116px;
      border-radius: 10px;
      border: 1px solid #cdced1;
      position: relative;
      padding: 13px 21px;
      cursor: pointer;

      .subject-box {
        position: absolute;
        right: 0;
        top: 0;
        display: flex;
        gap: 6px;

        .subject {
          display: inline-block;
          padding: 4px 10px;
          color: @color-brand-regular;
          background-color: @color-neutral-fill-light;
          border-radius: 0 0 14px 14px;
          font-size: 11px;

          &:last-child {
            border-radius: 0 10px 0 14px;
          }
        }
      }

      .people {
        position: absolute;
        display: inline-flex;
        align-items: center;
        right: 21px;
        bottom: 13px;
        font-size: 14px;
        gap: 4px;
        color: #040b1a66;
      }

      .info {
        display: flex;
        align-items: center;
        gap: 24px;
        font-size: 18px;
        font-weight: 700;

        img {
          width: 61px;
        }
      }

      &.active {
        background-color: #eef5ff;
        border-color: @color-brand-regular;

        .subject {
          background-color: #b2ccff;
        }

        .info {
          color: #1a70e8;
        }
      }
    }
  }

  .btn-box {
    text-align: right;
    .btn {
      background-color: unset;
      width: 140px;
      height: 47px;
      line-height: 47px;
      border: 1px solid @color-brand-regular;
      border-radius: 5px;
      margin-left: 20px;
      cursor: pointer;
    }

    .confirm {
      background-color: @color-brand-regular;
      color: #fff;
    }

    .cancel {
      color: @color-brand-regular;

      &:hover {
        background-color: #ecf5ff;
      }
    }
  }

  :deep(.el-drawer) {
    width: 546px !important;
    &__header {
      margin: 0;
    }

    &__title {
      font-size: 22px;
      font-weight: 700;
      color: #040b1acc;
    }

    &__body {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      gap: 20px;
    }
  }
}
</style>
